package com.cvdnn.aes.model;

import android.json.JSONUtils;
import android.math.AES;
import android.math.MD5;
import android.math.Maths;
import android.math.ShortDigest;

import org.json.JSONObject;

public abstract class Scheme {
    public static final String REG_ID = "120c83f760a7f64096e";
    public static final String CPU_SERIAL = "be25b7e4044604f5";
    public static final String PKG_NAME = "com.simple.green";

    public static final char ACTION_SEPARATOR = ':';

    protected String action;

    public abstract String scheme();

    public abstract void onFormatJson(JSONObject json, String dox, int box);

    public final String format(String dox, int box) {
        JSONObject json = new JSONObject();

        onFormatJson(json, dox, box);

//        JSONUtils.put(json, "rand_sn", unique());
//        JSONUtils.put(json, "t", System.currentTimeMillis() / 1000);

        // test
        JSONUtils.put(json, "rand_sn", "4pI7rCUpDkU");
        JSONUtils.put(json, "t", 1607939050l);

        return makeup(scheme(), json.toString());
    }

    public Scheme action(String action) {
        this.action = action;

        return this;
    }

    /**
     * AS=MD5（reg_id的值+uuid的值）
     *
     * @return
     */
    public static String figure() {

        return MD5.encrypt(PKG_NAME);
    }

    public static String makeup(String scheme, String text) {
        return AES.encrypt(figure(), scheme + ACTION_SEPARATOR + text);
    }

    public static String unique() {

        return ShortDigest.encrypt(MD5.encrypt(PKG_NAME + System.currentTimeMillis() + Maths.random(0, 999999)));
    }
}
